package com.xiaoxin.nmzp.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.nmzp.EduEx;
import com.ruoyi.common.nmzp.ProjectEx;
import com.ruoyi.common.nmzp.WorkEx;
import com.xiaoxin.nmzp.server.entity.domain.NmzpJob;
import com.xiaoxin.nmzp.server.entity.domain.NmzpJobResume;
import com.xiaoxin.nmzp.server.entity.domain.NmzpResume;
import com.xiaoxin.nmzp.server.entity.domain.SysUser;
import com.xiaoxin.nmzp.server.entity.req.SendReq;
import com.xiaoxin.nmzp.server.mapper.NmzpJobResumeMapper;
import com.xiaoxin.nmzp.server.mapper.NmzpResumeMapper;
import com.xiaoxin.nmzp.server.service.NmzpResumeService;
import com.xiaoxin.nmzp.utils.UserUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author xiaoxin
* @description 针对表【nmzp_resume】的数据库操作Service实现
* @createDate 2024-10-21 18:04:26
*/
@Service
public class NmzpResumeServiceImpl extends ServiceImpl<NmzpResumeMapper, NmzpResume>
    implements NmzpResumeService{
    @Autowired
    private NmzpJobResumeMapper nmzpJobResumeMapper;

    /**
     * 获取用户简历
     *
     * @return
     */
    @Override
    public NmzpResume getResume() {
        LambdaQueryWrapper<NmzpResume> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NmzpResume::getUserId, UserUtils.get().getUserId());
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 更新个人优势
     *
     * @param advantage
     */
    @Override
    public void updateAdvantage(String advantage) {
        LambdaUpdateWrapper<NmzpResume> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NmzpResume::getUserId, UserUtils.get().getUserId());
        updateWrapper.set(NmzpResume::getAdvantage, advantage);
        baseMapper.update(updateWrapper);
    }

    /**
     * 更新工作经历
     *
     * @param workEx
     */
    @Override
    public void updateWorkEx(WorkEx workEx) {
        String workExs = "";
        try {
           workExs =  new ObjectMapper().writeValueAsString(workEx);
        } catch (JsonProcessingException e) {
            throw new ServiceException("更新失败！");
        }
        LambdaUpdateWrapper<NmzpResume> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NmzpResume::getUserId, UserUtils.get().getUserId());
        updateWrapper.set(NmzpResume::getWorkExperience,workExs);
        baseMapper.update(updateWrapper);
    }

    /**
     * 更新项目经验
     *
     * @param projectEx
     */
    @Override
    public void updateProjectEx(ProjectEx projectEx) {
        String projectExStr = "";
        try {
            projectExStr =  new ObjectMapper().writeValueAsString(projectEx);
        } catch (JsonProcessingException e) {
            throw new ServiceException("更新错误");
        }
        LambdaUpdateWrapper<NmzpResume> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NmzpResume::getUserId, UserUtils.get().getUserId());
        updateWrapper.set(NmzpResume::getProjectExperience,projectExStr);
        baseMapper.update(updateWrapper);
    }

    /**
     * 更新学历信息
     *
     * @param eduEx
     */
    @Override
    public void updateEduEx(EduEx eduEx) {
        String eduExStr = "";
        try {
            eduExStr =  new ObjectMapper().writeValueAsString(eduEx);
        } catch (JsonProcessingException e) {
            throw new ServiceException("更新错误");
        }
        LambdaUpdateWrapper<NmzpResume> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NmzpResume::getUserId, UserUtils.get().getUserId());
        updateWrapper.set(NmzpResume::getEducations,eduExStr);
        baseMapper.update(updateWrapper);
    }

    /**
     * 投递简历
     *
     * @param sendReq
     */
    @Override
    public void sendResume(SendReq sendReq) {
        NmzpJobResume nmzpJobResume = new NmzpJobResume();
        nmzpJobResume.setJobId(sendReq.getJobId());
        NmzpResume resume = getResume();
        nmzpJobResume.setResumeId(resume.getResumeId());
        nmzpJobResume.setStatus("1");
        LambdaQueryWrapper<NmzpJobResume> jobResumeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        jobResumeLambdaQueryWrapper.eq(NmzpJobResume::getJobId, sendReq.getJobId());
        jobResumeLambdaQueryWrapper.eq(NmzpJobResume::getResumeId, resume.getResumeId());
        if (ObjectUtils.isNotEmpty(nmzpJobResumeMapper.selectList(jobResumeLambdaQueryWrapper))){
            throw new ServiceException("已投递过该岗位！", HttpStatus.SUBMIT_AGAIN);
        }
        nmzpJobResumeMapper.insert(nmzpJobResume);
    }
}




